package com.sayice.coursetable.ui.course.week;

import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sayice.coursetable.R;
import com.sayice.coursetable.databinding.CourseDetailBinding;
import com.sayice.coursetable.logic.model.Course;

import java.util.List;

/**
 * 周课程适配器
 */
public class WeekCourseAdapter extends RecyclerView.Adapter<WeekCourseAdapter.ViewHolder> {
    private final List<Course> courseList;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView courseInfo;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            courseInfo = itemView.findViewById(R.id.courseInfo);
        }
    }

    public WeekCourseAdapter(@NonNull List<Course> courseList) {
        this.courseList = courseList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
        View view = layoutInflater.inflate(R.layout.week_course_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.itemView.setOnClickListener(v -> {
            int position = viewHolder.getBindingAdapterPosition();
            Course course = courseList.get(position);
            if (course != null) {
                CourseDetailBinding detailBinding = CourseDetailBinding.inflate(layoutInflater);
                if (course.teach.matches(".*\\d+.*")) {
                    detailBinding.teachHint.setText("班级：");
                } else {
                    detailBinding.teachHint.setText("教师：");
                }
                detailBinding.name.setText(course.name);
                detailBinding.room.setText(course.room);
                detailBinding.teach.setText(course.teach);
                detailBinding.weeks.setText(course.weeks);
                new AlertDialog.Builder(viewGroup.getContext())
                        .setView(detailBinding.getRoot())
                        .show();
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        Course course = courseList.get(position);
        viewHolder.courseInfo.setText(course == null ? "" : course.name + "@" + course.room);
    }

    @Override
    public int getItemCount() {
        return courseList.size();
    }
}
